import os
from daemons import Daemons
import packages
from copy_files import CopCopy
from threading import Thread
import subprocess
from time import sleep

class Installer:
    @staticmethod
    def _install_with_opts(*args):
        gt = False
        dpath = '/'
        t = os.getcwd().split('/')[1:3]
        for u in t:
            u+='/'
            dpath+=u
        path = os.getcwd() + '/'
        copycopy = CopCopy(path=path, dpath=dpath)
        if args[0][0]: 
            Installer.__updatedb()
        Installer.__install_basepac()
        if args[0][1]: 
            Installer.__install_pacaddit()
        Installer.__install_aur()
        Installer.__install_aurbase()
        if args[0][2]:
            Installer.__install_auraddit()
            copycopy._copyetcher()
        Installer.__create_default_folders()
        copycopy._copy_base()
        if args[0][3]: 
            Installer.__is_laptop()
            copycopy._copylap()
            gt = True
        else:
            copycopy._copypc()
        copycopy._copyother()
        if args[0][4]:
            Installer.__install_drova()
        if args[0][5]:
            Installer.__install_waltg(path=path)
        if args[0][6]:
            Installer.__install_pywalfox()
        Daemons.enable_all_daemons(gt=gt)
        Installer.__install_fish()
        copycopy.copy_fish()
        Installer.__start_post(path=path)

    @staticmethod
    def __updatedb():
        os.system('sudo pacman -Sy')
        print   ('[+]')
    @staticmethod
    def __install_pacaddit():
        print('[---] Installing additional soft')
        for package in packages.ADDITI_PACMAN:
            os.system(f'sudo pacman -S --noconfirm {package}')
            print(f'[+] Installed: {package}')
    @staticmethod
    def __install_basepac():
        print('[---] Installing base soft')
        for package in packages.BASE_PACKETS_PACMAN:
            os.system(f'sudo pacman -S --noconfirm {package}')
            print(f'[+] Installed: {package}')
    @staticmethod
    def __install_aur():
        print('[---] Installing yay')
        os.system("git -C /tmp clone https://aur.archlinux.org/yay.git")
        os.system("cd /tmp/yay && makepkg -si --noconfirm")
        print('[+] Installed: yay')
    @staticmethod
    def __create_default_folders():
        print('[---] Create default folders')
        default_folders = "~/Videos ~/Documents ~/Downloads ~/Music ~/Desktop ~/Books ~/Pictures/screenshots ~/Pictures/walpapes ~/Projects "
        os.system("mkdir -p ~/.config/hypr/")
        os.system("mkdir -p ~/.config/gtk-4.0")
        os.system(f"mkdir -p {default_folders}")
        os.system("sudo mkdir -p /usr/share/applications/")
        os.system("sudo mkdir -p /usr/share/fonts")
        os.system("sudo mkdir -p /usr/share/icons")
        os.system("sudo mkdir -p /usr/share/themes")
        os.system("sudo mkdir -p /root/.config/gtk-4.0")
        print('[+] Folders created!')
    @staticmethod
    def __install_aurbase():
        print('[---] Installing base aur soft')
        for package in packages.AUR_PACKAGES:
            os.system(f"yay -S --noconfirm {package}")
            print(f'[+] Installed: {package}')
    @staticmethod
    def __install_auraddit():
        print('[---] Installing additional aur soft')
        for package in packages.ADDITI_AUR:
            os.system(f'yay -S --noconfirm {package}')
            print(f'[+] Installed: {package}')
    @staticmethod
    def __is_laptop():
        print('[---] Installing additional for laptop')
        os.system('sudo pacman -S --noconfirm power-profiles-daemon power-profiles-daemon-openrc')
        print('[+] Installed: lap-deps')
    @staticmethod
    def __install_drova():
        print('[---] Installing drivers')
        os.system('sudo pacman -S --noconfirm mesa xf86-video-nouveau xf86-video-intel vulkan-intel')
        print('[+] Installed: drivers')
    
    @staticmethod
    def __install_fish():
        command = 'fish'
        process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        sleep(2)  # Ждем 2 секунды
        process.terminate()  # Завершаем процесс
        output, error = process.communicate()

    @staticmethod
    def __install_waltg(path):
        print('[---] Installing wal-telegram')
        os.system(f'cd {path}wal-telegram/')
        os.system(f'sudo cp -r {path}wal-telegram/share/ /usr/share/wal-telegram')
        os.system(f'sudo cp -r {path}wal-telegram/wal-telegram /usr/bin/wal-telegram')
        os.system('sudo chmod +x /usr/share/wal-telegram/wal-telegram')
        os.system('sudo chmod +x /usr/bin/wal-telegram')
        os.system(f'cd {path}')
        print('[+] Installed wal-telegram')
    @staticmethod
    def __install_pywalfox():
        print('[---] Installing pywalfox')
        os.system('yay -S --noconfirm python-pywalfox')
        print('[+] Installed pywalfox')
    @staticmethod
    def __starthyp():
        process = subprocess.Popen(['Hyprland',], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, error = process.communicate()

    @staticmethod
    def __post_bin(path):
        os.system(f'sakura -x {path}ggt')
    @staticmethod
    def __start_post(path):
        threadtw = Thread(target=Installer.__post_bin(path=path))
        Installer.__starthyp()
        sleep(3)
        threadtw.start()
    
